var lastsel;
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'bss/apcost/list?state=isdelno&pGuid='+ GetUrlParam("guid"),
        datatype: "json",
        colModel: [			
			{ label: 'guid', name: 'guid', index: 'guid', width: 50, key: true ,hidden: true },

			{ label: '序号', name: 'pCNum', index: 'p_c_num', width: 80 }, 			
			{ label: '其他费用明细', name: 'pCDetail', index: 'p_c_detail', width: 80 }, 			
			{ label: '计取依据及说明', name: 'pCExplain', index: 'p_c_explain', width: 80 }, 			
			{ label: '排序号', name: 'pCSort', index: 'p_c_sort', width: 80 }, 			
			{ label: '是否汇总项', name: 'pCToll', index: 'p_c_toll', width: 80 }, 			
			{ label: '合价(万元)', name: 'pCPrice', index: 'p_c_price', width: 80,
                editable: true,
                editrules: { required: true, number: true, custom: true, custom_func: ValidateTvalue },
                edittype: "text"
			},
			{ label: '备注', name: 'pCRemarks', index: 'p_c_remarks', width: 80 }, 			

        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        footerrow:true,
        // multiselect: true,
        onSelectRow: EditSelectRow,
        // onSelectRow : function(guid) {
        //
        //     if (guid && guid !== lastsel) {
        //
        //
        //         var rowData = $("#jqGrid").jqGrid("getRowData", guid);
        //
        //         $('#jqGrid').jqGrid('restoreRow',lastsel);
        //
        //
        //     }
        // },

        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
            var sum_qntqFy=$("#jqGrid").getCol('pCPrice',false,'sum');
            $("#jqGrid").footerData('set', {   'pCPrice': sum_qntqFy.toFixed(3) });

            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});
//选中行启用行编辑
var selectRowId=""
function EditSelectRow(id)
{
    //原选中行ID
    var oldSelectRowId = selectRowId;
    if (oldSelectRowId != null && oldSelectRowId != "" && oldSelectRowId.length > 0) {
       // $("#jqGrid").jqGrid('saveRow', oldSelectRowId);

        $('#jqGrid').jqGrid('saveRow',oldSelectRowId,{

                        keys : true,		//这里按[enter]保存
                        url: baseURL  + "bss/apcost/updateprice",
                        mtype : "POST",
                        restoreAfterError: true,
                        extraparam: {

                        },
                        oneditfunc: function(oldSelectRowId){
                            console.log(1111);
                        },
                       aftersavefunc: function(oldSelectRowId) {
                           var sum_qntqFy=$("#jqGrid").getCol('pCPrice',false,'sum');
                           $("#jqGrid").footerData('set', {   'pCPrice': sum_qntqFy.toFixed(3) });

                       },
                        succesfunc: function(response){
                           return true;
                        },
                        errorfunc: function(oldSelectRowId, res){
                            console.log(222);

                        }
                    });

    }

    //当前选中行
    selectRowId=id;//临时存储当前选中行
    //$("#fieldGrid").jqGrid('editRow', id);


    $('#jqGrid').jqGrid('editRow',id,{

        keys : true,		//这里按[enter]保存
        url: baseURL  + "bss/apcost/updateprice",
        mtype : "POST",
        focusField: 1,
        restoreAfterError: true,
        extraparam: {

        },
        oneditfunc: function(id){
            console.log(1111);
        },
        aftersavefunc: function(id) {
            var sum_qntqFy=$("#jqGrid").getCol('pCPrice',false,'sum');
            $("#jqGrid").footerData('set', {   'pCPrice': sum_qntqFy.toFixed(3) });

        },
        succesfunc: function(response){
         return true;
        },
        errorfunc: function(id, res){
            console.log(222);

        }
    });


}
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        q:{

            pCToll:null
        },

		aPCost: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.aPCost = {};
		},
		update: function (event) {
			var guid = getSelectedRow();
			if(guid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(guid)
		},
		saveOrUpdate: function (event) {
			var url = vm.aPCost.guid == null ? "bss/apcost/save" : "bss/apcost/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.aPCost),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var guids = getSelectedRows();
			if(guids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "bss/apcost/delete",
                    contentType: "application/json",
				    data: JSON.stringify(guids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(guid){
			$.get(baseURL + "bss/apcost/info/"+guid, function(r){
                vm.aPCost = r.aPCost;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{

                    'pCToll': vm.q.pCToll,
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});